import type { CSSProperties, Ref, SetupContext } from 'vue'
import { computed, nextTick, getCurrentInstance, reactive } from 'vue'
import { VTableProps, VtableEmits } from './vtableProps'
import { VTableRef } from './table'
import { hasSelected } from './rowspan'

// 重写 element table  相关事件
export const useElementEvent = (props: VTableProps, targetRef: VTableRef) => {
  const instance = getCurrentInstance()!

  const toggleRowSelection = (row: any, selected: any) => {
    targetRef!.value.toggleRowSelection(row, selected)
  }

  const setCurrentRow = (row: any) => targetRef.value.setCurrentRow(row)

  const toggleAllSelection = () => targetRef.value.toggleAllSelection()
  const setScrollTop = (top?: number) => targetRef.value?.setScrollTop(top)

  function getSelectionRows() {
    const tabelData = instance.proxy?.data
    const selections = targetRef.value.getSelectionRows()

    if (props.selectSort) {
      /**
       * 根据表格顺序返回选中的数据
       */
      const sortSelections: any = []
      tabelData.map((row: any) => {
        if (hasSelected(selections, row)) sortSelections.push(row)
      })

      return sortSelections
    }
    return selections
  }

  return {
    toggleRowSelection,
    getSelectionRows,
    setCurrentRow,
    toggleAllSelection,
    setScrollTop,
  }
}
